
global data_folder "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results"

*MERGE INFORMATION ON OLD AND CURRENT MUNICIPALITY CODES
use C1 C2 C4 C5 using "D:\metadata\classifications\region\lakkaut_voim_olevat_kunnat_19.dta", clear
drop if _n<=2
ren C1 kunta_old
ren C2 Municipality
ren C4 kunta_new
ren C5 Municipality_new
sort Municipality
save $data_folder\munic_mergers_old, replace

use C1 C2 C4 C5 using "D:\metadata\classifications\region\lakkaut_voim_olevat_kunnat_19.dta", clear
drop if _n<=2
ren C1 kunta_old
ren C2 Municipality_old
ren C4 kunta_new
ren C5 Municipality_new
keep Municipality_new kunta_new
duplicates drop 
rename Municipality_new Municipality
sort Municipality
save $data_folder\munic_mergers_onlynew, replace

*MAPPINF OF MUNICIPALITY NAMES AND CODES
*import excel "D:\metadata\classifications\region\kunta_1_20050101#nuts_1_20030711.xlsx", sheet("Koodit ja nimet") clear
*the file below is the same as above but took off the first variable
import excel "W:\shelter_vote_data\kunta_1_20050101_mapping.xlsx", sheet("Koodit ja nimet") firstrow clear
rena luokannimi Municipality
renam luokankoodi kunta
keep Municipality kunta
duplicates drop
sort Municipality
save $data_folder\munic_names, replace


**************************MUNICIPALITY ELECTION VOTE SHARES**********************************
use "W:\c55_2024_01_24_sisaan\kuntavaalit.dta", clear
duplicates report Municipality Year

sort Municipality
merge Municipality using $data_folder\munic_names, nokeep
tab _merge
rename Year year
drop _merge
tab Municipality if kunta==""
*merge codes for new municipalities for which the code is still missing
sort Municipality
merge Municipality using $data_folder\munic_mergers_onlynew, nokeep update 
tab _merge
drop _merge
count if kunta==""
replace kunta=kunta_new if kunta==""
*merge codes for old municipalitities if name still missing 
tab Municipality if kunta_new=="" | kunta==""
sort Municipality
merge Municipality using $data_folder\munic_mergers_old, nokeep update
tab _merge
drop _merge
count if kunta_new==""
count if kunta==""
count if kunta_old==""
tab Municipality if (kunta_new=="" & kunta=="" & kunta_old=="" )
replace kunta=kunta_old if kunta==""
tab Municipality if kunta==""
drop if kunta==""
duplicates report Municipality year
duplicates report kunta year
tab year
rename muut_kunt muut_local
collapse (sum) muut_local *_kunt , by(kunta year)
*right-wing and conservative shares -parliamentary elections
*drop those for which we do not have information 
gen total=muut_local+KOK_kunt+SKP_kunt+PS_kunt+KESK_kunt+VIHR_kunt+VAS_kunt+RKP_kunt+KD_kunt+SDP_kunt+LIIKE_kunt
gen cons_kunt=PS_kunt+KESK_kunt+KOK_kunt+KD_kunt
gen ultracons_kunt=PS_kunt+KD_kunt
gen rightwing_kunt=PS_kunt
drop if total==0
tab year
keep if year>=2000
tab year
*simplify and use just the year of election votes shares. merge these to main data using just year codes. 
drop if kunta==""
duplicates report
sort kunta year
*if save the data at yearly level
preserve
forvalues j= 2000(4)2012 {
restore
preserve
keep if year==`j'
rename ultracons_kunt ultracons_kunt`j'
rename cons_kunt cons_kunt`j'
rename rightwing_kunt rightwing_kunt`j'
sort kunta
save $data_folder\voteshares_local_mun_simple_`j', replace
}



